#ifndef __TimerDef_C
#define __TimerDef_C

#include "TimerDef.h"

// PWM时钟设置：TM3
void Tmr_PWM_init(void){
	  TMR_BaseConfig_T TMR_TimeBaseStruct;
    TMR_OCConfig_T OCcongigStruct;

	  PWM_RCM_EnableAPBnPeriphClock(Tmr_PWM_RTC);
	  
	  TMR_TimeBaseStruct.clockDivision = TMR_CLOCK_DIV_1;
    TMR_TimeBaseStruct.countMode = TMR_COUNTER_MODE_UP;
    TMR_TimeBaseStruct.division = timerDiv;       //1M
    TMR_TimeBaseStruct.period = TmrPeriod_PWM;    //1K
    TMR_ConfigTimeBase(Tmr_PWM_Id, &TMR_TimeBaseStruct);
	
	  
	  OCcongigStruct.mode       = TMR_OC_MODE_PWM1;          //PWM设置	
    OCcongigStruct.idleState  = TMR_OC_IDLE_STATE_RESET;    
    OCcongigStruct.nIdleState = TMR_OC_NIDLE_STATE_RESET; 
	  
	  OCcongigStruct.polarity   = TMR_OC_POLARITY_HIGH;
    OCcongigStruct.nPolarity  = TMR_OC_NPOLARITY_HIGH;
	  
	  OCcongigStruct.outputState  = TMR_OC_STATE_ENABLE;
    OCcongigStruct.outputNState = TMR_OC_NSTATE_DISABLE;    //TMR_OC_NSTATE_ENABLE
    
		#ifdef AT32F403AVGT7	
		TMR_ConfigCompareSetn(Tmr_PWM_Id,PWM_Pulse); 
    #else		
		OCcongigStruct.pulse        = PWM_Pulse; //比较脉冲数
		#endif
	
	  // PWM输出
    PWM_TMR_ConfigOCn(Tmr_PWM_Id, &OCcongigStruct);		
    PWM_TMR_ConfigOCnPreload(Tmr_PWM_Id, TMR_OC_PRELOAD_ENABLE); 
		
    #if CP_CarD1_Trigger_Mode == CP_CarD1_Trigger_CC50CC950		
			// adc触发：occ2
			ADC_TMR_ConfigOCn(Tmr_PWM_Id, &OCcongigStruct);	
			ADC_TMR_ConfigOCnPreload(Tmr_PWM_Id, TMR_OC_PRELOAD_ENABLE); 
			ADC_ConfigCompareSetn(Tmr_PWM_Id,TmrPeriod_ADCDelay);
						
			CarD1_TMR_ConfigOCn(Tmr_PWM_Id, &OCcongigStruct);	
			CarD1_TMR_ConfigOCnPreload(Tmr_PWM_Id, TMR_OC_PRELOAD_ENABLE); 
			CarD1_ConfigCompareSetn(Tmr_PWM_Id,950);
		#endif
				
		#ifdef APM32E10X_HD
			TMR_EnableAUTOReload(Tmr_PWM_Id); 
		#else
			TMR_EnableAutoReload(Tmr_PWM_Id); 			
		#endif
		
		#ifndef AT32F403AVGT7
			TMR_EnablePWMOutputs(Tmr_PWM_Id);	
		#endif
		
		// TMR3的中断：
		#if CP_CarD1_Trigger_Mode == CP_CarD1_Trigger_CCPWMUPDT  || CP_CarD1_Trigger_Mode == CP_CarD1_Trigger_TMRDelay  
			TMR_EnableInterrupt(Tmr_PWM_Id, PWM_CC_INT_FLAG);    
			TMR_EnableInterrupt(Tmr_PWM_Id, TMR_INT_UPDATE);		
    #else
			TMR_EnableInterrupt(Tmr_PWM_Id, CarD1_INT_FLAG);	
			TMR_EnableInterrupt(Tmr_PWM_Id, ADC_INT_FLAG);
		#endif
		
		#if CP_CarD1_Trigger_Mode == CP_CarD1_Trigger_CCPWMUPDT  || CP_CarD1_Trigger_Mode == CP_CarD1_Trigger_TMRDelay
			NVIC_EnableIRQRequest(PWM_TMR_UP_IRQn, 1, 0);
		#endif
		
    NVIC_EnableIRQRequest(Tmr_PWM_IRQn, 1, 0);
		
		//使能输出，高级定时器特有。
		#ifdef AT32F403AVGT7
		tmr_output_enable(Tmr_PWM_Id,TRUE); 
		#endif
		
		TMR_Enable(Tmr_PWM_Id);
				
}

// 占空比设置：单位=%; dutyValue > 1001时，采用pwmDuty保存的值。
// 否则，直接用pwmDuty值刷新
void PWM_Duty_Set(uint16_t dutyValue){
	  #if RTC_ON > 0
		getTimestampRTC();
		#endif
    if(system_time - Relay_Chg_tm < relay_stable_time) return;	
		PWM_Data.pwmDuty = dutyValue;
		if(PWM_Data.pwmDutyPre != PWM_Data.pwmDuty){  
			  TMR_ConfigCompareSetn(Tmr_PWM_Id,PWM_Data.pwmDuty); 
			  //TMR_ConfigCompareSetn(Tmr_PWM_Id,267);
		    PWM_Data.pwmDutyPre = PWM_Data.pwmDuty;			  
		}
}

// 全局时钟定时器：Tmr2
#if RTC_ON == 0 || CP_CarD1_Trigger_Mode == CP_CarD1_Trigger_TMRDelay
void Tmr_CLOCK_init(void){	  
	  TMR_BaseConfig_T TMR_BaseConfigStruct; 
    CLOCK_RCM_EnableAPBnPeriphClock(Tmr_CLOCK_RTC); 
    TMR_BaseConfigStruct.countMode = TMR_COUNTER_MODE_UP;  
    TMR_BaseConfigStruct.division = timerDiv;                // 1M
	  #if CP_CarD1_Trigger_Mode == CP_CarD1_Trigger_TMRDelay
    TMR_BaseConfigStruct.period = TmrPeriod_ADCDelay;        // 50us
	  #else
	  TMR_BaseConfigStruct.period = TmrPeriod_CLOCK;           // 1ms
	  #endif
    TMR_ConfigTimeBase(Tmr_CLOCK_Id, &TMR_BaseConfigStruct); 
    TMR_EnableInterrupt(Tmr_CLOCK_Id, TMR_INT_UPDATE); 
    NVIC_EnableIRQRequest(Tmr_CLOCK_IRQn, 1, 0);
    #if CP_CarD1_Trigger_Mode != CP_CarD1_Trigger_TMRDelay	
    TMR_Enable(Tmr_CLOCK_Id);	
	  #endif
}	
#endif
	
// ADC 数据提取寄存器: TM3
#if UI_Meter_Method == Meter_SelfADC || CP_Mode == CP_Mode_sta
void Tmr_ADC_init(void){	  
	  TMR_BaseConfig_T TMR_BaseConfigStruct;
    #if RlyPin_Ctrl_PWM_EN > 0 && (defined SQDri_WS018B || defined SQDri_WS018)
	  TMR_OCConfig_T OCcongigStruct;
	  #endif
	
	  // 时钟设置
    ADC_RCM_EnableAPBnPeriphClock(Tmr_ADC_RTC); 
	
    TMR_BaseConfigStruct.clockDivision = TMR_CLOCK_DIV_1;  
    TMR_BaseConfigStruct.countMode = TMR_COUNTER_MODE_UP;  
    TMR_BaseConfigStruct.division = timerDiv;            // 1M = 1us
	  #if RlyPin_Ctrl_PWM_EN > 0 && (defined SQDri_WS018B || defined SQDri_WS018)
    TMR_BaseConfigStruct.period = PWMPeriod_Relay;       //50us = 0.05ms;
	  #else
	  TMR_BaseConfigStruct.period = TmrPeriod_ADC;         //100us = 0.1ms;
	  #endif
    TMR_ConfigTimeBase(Tmr_ADC_Id, &TMR_BaseConfigStruct);
	  
	  // 输出PWM波
	  #if RlyPin_Ctrl_PWM_EN > 0 && (defined SQDri_WS018B || defined SQDri_WS018)
			OCcongigStruct.mode       = TMR_OC_MODE_PWM2;          //PWM设置	
			OCcongigStruct.idleState  = TMR_OC_IDLE_STATE_RESET;    
			OCcongigStruct.nIdleState = TMR_OC_NIDLE_STATE_RESET;
			OCcongigStruct.polarity   = TMR_OC_POLARITY_HIGH;
			OCcongigStruct.nPolarity  = TMR_OC_NPOLARITY_HIGH;	  
			OCcongigStruct.outputState  = TMR_OC_STATE_ENABLE;
			OCcongigStruct.outputNState = TMR_OC_NSTATE_ENABLE;    
			#ifdef AT32F403AVGT7	
			Relay_ConfigCompareSetn(Tmr_Relay_Id,Relay_DutyPulse_init); 
			#else		
			OCcongigStruct.pulse = Relay_DutyPulse_init; //比较脉冲数
			#endif	
			// PWM输出
			Relay_TMR_ConfigOCn(Tmr_Relay_Id, &OCcongigStruct);		
			Relay_TMR_ConfigOCnPreload(Tmr_Relay_Id, TMR_OC_PRELOAD_ENABLE); 

			#ifdef APM32E10X_HD
				TMR_EnableAUTOReload(Tmr_Relay_Id); 
			#else
				TMR_EnableAutoReload(Tmr_Relay_Id); 			
			#endif
			
			#ifndef AT32F403AVGT7
				TMR_EnablePWMOutputs(Tmr_Relay_Id);	
			#endif
		
	  #endif //RlyPin_Ctrl_PWM_EN > 0 && (defined SQDri_WS018B || defined SQDri_WS018)
	
    TMR_EnableInterrupt(Tmr_ADC_Id, TMR_INT_UPDATE);	
    NVIC_EnableIRQRequest(Tmr_ADC_IRQn, 2, 0);
    		
    TMR_Enable(Tmr_ADC_Id);	
}
#endif

#ifndef UsingFreeRTOS
// 执行任务的时钟；单次执行:TMR5 或 TMR4
void Tmr_Task_init(void){
	  TMR_BaseConfig_T TMR_BaseConfigStruct;	
    Task_RCM_EnableAPBnPeriphClock(Tmr_Task_RTC); 
    TMR_BaseConfigStruct.countMode = TMR_COUNTER_MODE_UP; 
    TMR_BaseConfigStruct.division = timerDiv;           // 1M
    TMR_BaseConfigStruct.period = TmrPeriod_Task;       // 10ms执行一次任务
    TMR_ConfigTimeBase(Tmr_Task_Id, &TMR_BaseConfigStruct); 		
    TMR_EnableInterrupt(Tmr_Task_Id, TMR_INT_UPDATE);	
    NVIC_EnableIRQRequest(Tmr_Task_IRQn, 2, 1);
    TMR_Enable(Tmr_Task_Id);
	
}
#endif


#if RlyPin_Ctrl_PWM_EN > 0
// PWM控制Relay继电器引脚的时钟；TMR4
#if defined SQDri_EVSEB_7KW || defined SQDri_EVSEB_11KW  
void Tmr_Relay_init(void){
	  TMR_BaseConfig_T TMR_BaseConfigStruct;	
	  TMR_OCConfig_T OCcongigStruct;
	
    Relay_RCM_EnableAPBnPeriphClock(Tmr_Relay_RTC); 
	  
	  TMR_BaseConfigStruct.clockDivision = TMR_CLOCK_DIV_1;
    TMR_BaseConfigStruct.countMode = TMR_COUNTER_MODE_UP; 
    TMR_BaseConfigStruct.division = timerDiv;            // 1M
    TMR_BaseConfigStruct.period = PWMPeriod_Relay;       // PWM的频率;
    TMR_ConfigTimeBase(Tmr_Relay_Id, &TMR_BaseConfigStruct); 	
	
    OCcongigStruct.mode       = TMR_OC_MODE_PWM1;          //PWM设置	
    OCcongigStruct.idleState  = TMR_OC_IDLE_STATE_RESET;    
    OCcongigStruct.nIdleState = TMR_OC_NIDLE_STATE_RESET;
	  OCcongigStruct.polarity   = TMR_OC_POLARITY_HIGH;
    OCcongigStruct.nPolarity  = TMR_OC_NPOLARITY_HIGH;	  
	  OCcongigStruct.outputState  = TMR_OC_STATE_ENABLE;
    OCcongigStruct.outputNState = TMR_OC_NSTATE_DISABLE;    //TMR_OC_NSTATE_ENABLE    
		#ifdef AT32F403AVGT7	
		Relay_ConfigCompareSetn(Tmr_Relay_Id,Relay_DutyPulse_init); 
    #else		
		OCcongigStruct.pulse = Relay_DutyPulse_init; //比较脉冲数
		#endif	
	  // PWM输出
    Relay_TMR_ConfigOCn(Tmr_Relay_Id, &OCcongigStruct);		
    Relay_TMR_ConfigOCnPreload(Tmr_Relay_Id, TMR_OC_PRELOAD_ENABLE); 

    #ifdef APM32E10X_HD
			TMR_EnableAUTOReload(Tmr_Relay_Id); 
		#else
			TMR_EnableAutoReload(Tmr_Relay_Id); 			
		#endif
		
		#ifndef AT32F403AVGT7
			TMR_EnablePWMOutputs(Tmr_Relay_Id);	
		#endif
		
    TMR_Enable(Tmr_Relay_Id);
		
}
#endif

// dutyValue = 600;占空比 = 60%; 6.58V
void Relay_Duty_Set(uint16_t dutyValue){
    dutyValue = dutyValue / 20; 
		if(relay_duty != dutyValue){  
			  Relay_ConfigCompareSetn(Tmr_Relay_Id,dutyValue); 
		    relay_duty = dutyValue;
			  
		}
}

#endif //RlyPin_Ctrl_PWM_EN > 0



#endif




